﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using HYView;

namespace HYRElectric.WirePlace
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdConnectWire : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Document document = activeUIDocument.ActiveView.Document;
			if (!(document.ActiveView is ViewPlan))
			{
				Result result = SwitchView.SetUsableView((HYView.CmdViewType)3, commandData);
				if (result != Result.Succeeded)
				{
					return result;
				}
			}
			using (WireParamNon_modeForm wireParamNon_modeForm = new WireParamNon_modeForm(document, false))
			{
				wireParamNon_modeForm.Show(WindowHelper.HWndRevit);
				WindowHelper.SendMessageToWndRevit();
				for (;;)
				{
					try
					{
						EquipmentConnectWires.SelectEquipments(activeUIDocument);
					}
					catch (Exception)
					{
						wireParamNon_modeForm.Close();
						break;
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
